<?php


namespace app\controller;


use app\BaseController;
use think\facade\Db;
use app\model\Demo;

class Data extends BaseController
{
    public function index(){
		//通过门面模式来获取的
        $result = Db::table('mall_demo')
            ->where("id", 1)
            ->find();
        dump($result);
		//通过容器的方式来处理
        $result = app("db")->table('mall_demo')->where("id", 2)->find();
        dump($result);
		//排序
        $result = Db::table('mall_demo')
            ->order('id','desc')
            ->find();
        dump($result);
		//分页
        $result = Db::table('mall_demo')
            ->order('id','desc')
            //->limit(0,2)
            ->page(1,2)
            ->select();
        dump($result);
		//id > 2
        $result = Db::table('mall_demo')
            ->where('id', '>', 2)
            ->select();
        dump($result);
		//多个条件id > 2 且 category_id = 3
        $result = Db::table('mall_demo')
            ->where('id', ">", 2)
            ->where('category_id', '=', 3)
            ->select();
        dump($result);
		//多个条件id > 2 且 category_id = 3
        $result = Db::table('mall_demo')
            ->where([
                ['id', '>', 2],
                ['category_id', '=', 3]
            ])
            ->select();
        dump($result);
		//1,2,3,4中category_id = 3
        $result = Db::table('mall_demo')
            ->where([
                ['id', 'in', '1,2,3,4'],
                ['category_id', '=', 3]
            ])
            ->select();
        dump($result);
    }
    public function abc(){
		//第一种打印SQl
        $result = Db::table('mall_demo')
            ->where("id", 10)
            ->fetchSql()
            ->find();
        dump($result);
		//第二种打印SQl
        $result = Db::table('mall_demo')
            ->where("id", 10)
            ->find();
        echo Db::getLastSql(); exit;
    }
    public function demo(){
        $data = [
            "title" => "tianqi5",
            "content" => "tianqi5 来自北京"
        ];
		//新增逻辑
        $result = Db::table('mall_demo')->insert($data);
        echo Db::getLastSql();
        dump($result);
		//删除逻辑
        $result = Db::table('mall_demo')->delete(3);
        dump($result);
        $result = Db::table('mall_demo')->where('id', 5)->delete();
        dump($result);
		//更新操作
        $result = Db::table('mall_demo')->where('id', 2)->update(['title'=>'tianqi222']);
        dump($result);
    }
    public function model(){
		//返回对象
        $result = Demo::find(2);
        dump($result);
		//转成数组
        dump($result->toArray());
    }
    public function model2(){
        $modelObj = new Demo();
		//返回对象
        $results = $modelObj->where('category_id',3)
            ->limit(2)
            ->order('id','desc')
            ->select();
        foreach($results as $result){
            dump($result->content);
            dump($result['content']);
            dump($result->status_text);
        }
    }
}
